﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using GameStateManagement.Screens.Base;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using GameStateManagement.GameHelper;

namespace GameStateManagement.Mine
{
    public class MyMainScreen : ButtonScreen
    {

        Song background_sound;
        public MyMainScreen()
            : base("Main Menu")
        {
            MyButton playGameMenuEntry = new MyButton("Play Game") { TextureName = "BatDau" };
            MyButton optionsMenuEntry = new MyButton("Options") { TextureName = "ThietLap" };
            MyButton creditMenuEntry = new MyButton("Credit") { TextureName = "ThongTin" };
            MyButton exitMenuEntry = new MyButton("Exit") { TextureName = "Thoat" };
            MyButton loadGameEntry = new MyButton("") { TextureName = "LoadGame" };

            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            optionsMenuEntry.Selected += OptionsMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;
            creditMenuEntry.Selected += new System.EventHandler<MyEventArgs>(creditMenuEntry_Selected);
            loadGameEntry.Selected += new System.EventHandler<MyEventArgs>(loadGameEntry_Selected);


            Buttons.Add(playGameMenuEntry);

            if (GameSettings.Instance.LastLevel > 0)
                Buttons.Add(loadGameEntry);
            Buttons.Add(optionsMenuEntry);
            Buttons.Add(creditMenuEntry);
            Buttons.Add(exitMenuEntry);
        }

        void loadGameEntry_Selected(object sender, MyEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
                               new GameplayScreen() { StartupLevel = GameSettings.Instance.LastLevel });
        }

        void creditMenuEntry_Selected(object sender, MyEventArgs e)
        {

            ScreenManager.AddScreen(new CreditScreen(), e.PlayerIndex);
        }


        public override void LoadContent()
        {
            base.LoadContent();
            ContentManager content = this.ScreenManager.Game.Content;
            foreach (MyButton item in Buttons)
            {
                item.Load(this);
            }
            background_sound = content.Load<Song>("Sound\\backgroundsound");
            MediaPlayer.Play(background_sound);
            if (!GameSettings.Instance.AudioEnable)
                MediaPlayer.Pause();
        }

        public override void UnloadContent()
        {
            MediaPlayer.Stop();
            base.UnloadContent();
        }

        void PlayGameMenuEntrySelected(object sender, MyEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
                               new GameplayScreen() { StartupLevel = 0 });
        }


        void OptionsMenuEntrySelected(object sender, MyEventArgs e)
        {
            ScreenManager.AddScreen(new OptionsMenuScreen() , e.PlayerIndex);
        }

        protected override void OnCancel(PlayerIndex playerIndex)
        {
            const string message = "Are you sure you want to exit this game?";

            MyMessageBox confirmExitMessageBox = new MyMessageBox(message);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
        }
        
        void ConfirmExitMessageBoxAccepted(object sender, MyEventArgs e)
        {
            ScreenManager.Game.Exit();
        }


    }
}
